package com.xls.dormitoryservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.dormitoryservice.service.RepairService;
import com.xls.dormitoryservice.vo.RepairQuery;
import com.xls.dormitoryservice.vo.RepairVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/dormitory/repair")
@Api(value = "宿舍维修管理")
@Slf4j
public class RepairController {
    @Autowired
    private RepairService RepairService;

    @Log(title = "维修管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:repair:list','dormitory:repairInfo:list')")
    @GetMapping("/getRepair/{id}")
    public Result<RepairVo> getRepair(@PathVariable String id) {
        return Result.success(RepairService.getRepair(id));
    }

    @Log(title = "维修管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:repair:list','dormitory:repairInfo:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<RepairVo>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                        @RequestBody RepairQuery repairQuery) {
        return Result.success(RepairService.getRepairVoPage(page, limit, repairQuery));
    }

    @Log(title = "维修管理", businessType = "添加")
    @PreAuthorize("hasAnyAuthority('dormitory:repair:save','dormitory:repairInfo:list')")
    @PostMapping("/save")
    public Result<RepairVo> save(@RequestBody RepairVo repairVo) {
        return Result.success(RepairService.saveRepair(repairVo));
    }

    @Log(title = "维修管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('dormitory:repair:update')")
    @PostMapping("/update")
    public Result<RepairVo> update(@RequestBody RepairVo repairVo) {
        return Result.success(RepairService.updateRepair(repairVo));
    }

    @Log(title = "维修管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('dormitory:repair:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<RepairVo> remove(@PathVariable String id) {
        return RepairService.removeById(id) ? Result.success() : Result.error();
    }
}

